# 导入数据库模块
import pymysql
# 导入Flask框架，这个框架可以快捷地实现了一个WSGI应用
from flask import Flask

from flask import render_template
# 导入前台请求的request模块
from flask import request,Blueprint,session
import traceback

# 传递根目录
searchWI=Blueprint('searchWI',__name__)

# 获取登录参数及处理
@searchWI.route('/searchWorkerInfo', methods=['get', 'post'])
def unemploy():
     if session['isadminan']=='企业':
        return render_template('searchWorkerInfo.html')
     else:
         return '您没有此操作权限！'

def get_conn():

    conn = pymysql.connect(
        host='139.217.107.173', user='root', password='123456', db='project_training', charset='utf8')

    cursor = conn.cursor()
    return conn, cursor
    print("success")

def close(conn,cursor):
    conn.close()
    cursor.close()

@searchWI.route('/search')
def search():
    conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training',
                           charset='utf8')
    cur = conn.cursor()
    # sql = "SELECT * FROM workerinfo "
    # print(session["user"])
    sql = "SELECT * FROM workerinfo WHERE CName='%s'"%(session["user"])
    print(sql)
    cur.execute(sql)
    u = cur.fetchall()
    conn.close()
    lis = []
    for i in u:
        lis2 = []
        for i2 in i:
            if i2 is None:
                lis2.append('NULL')
            else:
                lis2.append(i2)
        lis.append(lis2)
    data = ''
    for i in lis:
        data = data + '{' + r'"id":' + r'"' + '%d' % i[0] + r'",' + r'"name":' + r'"' + i[
            1] + r'",' + r'"sex":' + r'"' + i[2]
        data = data + r'",' + r'"tel":' + r'"' + i[5] + r'",' + r'"email":' + r'"' + i[6] + r'",' + r'"major":' + r'"' + \
               i[9]
        data = data + r'",' + r'"school":' + r'"' + i[10] + r'",' + r'"education":' + r'"' + i[11] + r'"' + "},"
    data = data[:-1]
    str = r'{"code":0,"msg":"","count":100,"data":[' + data + r']}'
    # print(str)
    return str


